package cn.coldleaf.wiki.utils;

import com.baomidou.mybatisplus.core.toolkit.AES;

/**
 * Token 工具类
 * @datetime 2023/10/17 下午10:49
 * @user cold
 **/
public class TokenUtil {

    /**
     * 构建token
     * @datetime 2023-10-18 00:01:09
     * @user cold
     **/
    public static String genToken(long userId,String key){
        StringBuilder ret = new StringBuilder();
        ret.append(userId)
                .append('_')
                .append(System.currentTimeMillis());
        int sign = ret.hashCode();
        ret.append("_").append(sign);

        return AES.encrypt(ret.toString(),key);
    }

    /**
     * 从token获取userId
     * @datetime 2023-10-18 00:01:25
     * @user cold
     **/
    public static long getUserIdFromToken(String token,String key){
        String tmp = AES.decrypt(token, key);
        String [] strs = tmp.split("_");
        return Long.parseLong(strs[0]);
    }
}
